from catch.read_cook import read_cookies
import json
from urllib.request import ProxyHandler
from urllib.request import Request
from urllib.parse import urlencode
from urllib.request import urlopen
from urllib.request import build_opener
from urllib.request import install_opener
from urllib.error import URLError
from catch.read_file import read
from urllib.request import HTTPHandler

#根据保存的用户id，抓取对应id的粉丝

def get_fans(user_id,page):

    try:
        url='https://m.weibo.cn/api/container/getIndex'

        parm={}
        parm['containerid']='231051_-_fans_-_'+user_id
        parm['luicode']='10000011'
        parm['lfid']='100505'+user_id
        parm['type']='all'
        parm['page']=page

        head={'Accept':'application/json, text/plain, */*','Connection':'keep-alive',
              'Host':'m.weibo.cn',
              'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36',
              'X-Requested-With':'XMLHttpRequest',
              'Cookie':read_cookies()}

        proxy = ProxyHandler({'http': read()})
        opener = build_opener(proxy, HTTPHandler)
        install_opener(opener)

        get_url=url+'?'+urlencode(parm)
        request=Request(url=get_url,headers=head)
        respon=urlopen(request).read().decode('utf-8')
        data=json.loads(respon)
        res_data=data['data']
        cards=res_data['cards']
        fans_arr=[]
        for card in cards:
            if 'card_group' in card:
                card_group=card['card_group']
                for group in card_group:
                    user=group['user']
                    users_id=user['id']
                    users_name=user['screen_name']
                    users_fans=user['followers_count']
                    print('user_id:%s;user_name:%s;users_fans:%s'%(users_id,users_name,users_fans))
                    fans_arr.append(users_id)

        fans_file=open('../file/fans.txt','a')
        for i in fans_arr:
            fans_file.write(str(i)+'\n')
    except URLError as e:
        if hasattr(e,'code'):
            print(e.code)
        if hasattr(e,'reason'):
            print(e.reason)

if __name__=='__main__':
    file_user=open('../file/user.txt','r')
    user=file_user.readlines()
    for u in user:
        print('user',u)
        for i in range(1,10):
            print('正在抓取第%d页数据'%i)
            get_fans(u,i)